<?php

/**
 * @author dragon.hu add @2016.4.25
 */
defined('BASEPATH') or die('forbidden');
require_once APPPATH.'channels/ChargeOrder.php';

class resupplycporder_controller extends Controller {

	//private $inc;
	//private $allchannel;
	//private $platformList;
	private $tmpdb;
    function _init() {
        check_level(50, segment(0), segment(1), segment(2, 'index'));
        
        //$this->inc = $this->config['inc'];
        //$this->allchannel = $this->inc['allchannel'];
        //$this->platformList = array('0' => 'IOS越狱', '1' => '安卓');
        require (CONFIGPATH . 'db.php');
        $this->tmpdb = new Database($config['payment_db']);
    }

    function index() {
     if (is_submit()) {
            $rowdata = $_POST['row'];
            //var_dump($rowdata);
            //exit();
            $sql = "SELECT * FROM tt_payment WHERE orderid = '".$rowdata['orderid']."'";
			$one = $this->tmpdb->one($sql);
				// 第几次充值
			$paytimes = 0;
			$sql = "SELECT * FROM tt_payment WHERE serverid = " . $one['serverid'] . " AND roleid='" . $one ['roleid'] . "'";
			$this->tmpdb->query ( $sql );
			$rolepayarr = array ();
			while ( $n = $this->tmpdb->get_array () ) {
				$rolepayarr [] = $n;
			}
			for($i = 0; $i < count ( $rolepayarr ); $i ++) {
				if (isset ( $rolepayarr [$i] ['money'] )) {
					$paytimes += 1;
				}
			}
			//var_dump($one);
			//var_dump($paytimes);
			//exit();
			$now = date("Y-m-d H:i:s", time());
			
			// 订单信息更新成功 开始发放充值
			if ($one['status'] == 1 || $one['status'] == 4) {
				$serverInfo = $this->tmpdb->one ( "SELECT * FROM {pre}areasample WHERE `id`='" . $one['serverid'] . "'" );
				$tmpone ['orderid'] = ( string ) $one['orderid'];
				$tmpone ['roleid'] = ( int ) $one['roleid'];
				$tmpone ['channel'] = ( string ) $one['channel'];
				$tmpone ['money'] = ( string ) $one['money'];
				$tmpone ['platform'] = ( int ) $one['platform'];
				$tmpone ['finish_time'] = ( string ) $now;
				$tmpone ['product_id'] = ( int ) $one['product_id'];
				$tmpone ['paytimes'] = $paytimes; // 累计支付次数
				$datas [] = $tmpone;
				$json_str = json_encode ( $datas );
				$json_str = str_replace ( " ", "%20", $json_str );
				
				$strResult = vpost ( $serverInfo ['host'] . "json_str=$json_str&action=45", 10 ); // 请求游戏服务器发放充值
				                                                                              // 推送游戏服务器超时或者失败
				if ($strResult === FALSE || $strResult == "fail") {
					echo "<script>alert('send failed！！！')</script>";
				}
				else
				{
					echo "<script>alert('success！！！')</script>";
				}
			} else {
				echo "<script>alert('not STATUS_SUCCESS！！！')</script>";
			}
        }
        
        $this->view->display("seal/resupplycporder/index");
    }

}

?>
